package com.linran.structure_algorithm.数据结构.lesson6_栈;

/**
 * 栈
 */
public class StackLearn<T> {
    Object[] elements;
    int top = 0;
    int maxSize;

    //最大数量
    public StackLearn(int maxSize) {
        this.maxSize = maxSize;
        this.elements = new Object[maxSize];
    }

    //是否为空
    public boolean isEmpty() {
        return top <= 0;
    }

    //是否栈满
    public boolean isFull() {
        return top >= maxSize;
    }

    //放入元素
    public void push(T element) {
        if (isFull()) {
            System.out.println("元素已满，不能放入");
            return;
        }
        elements[top] = element;
        top++;
    }

    //取出元素
    public T pop() {
        if (isEmpty()) {
            System.out.println("栈已空");
            return null;
        }
        top--;
        return (T)elements[top];
    }

    /*返回栈顶元素*/
    public T firstElement() {
        int firstIndex = top - 1;
        if (firstIndex < 0) {
            return null;
        }
        return (T)elements[firstIndex];
    }

    /*返回栈中元素数量*/
    public Integer size() {
        return top;
    }
}
